package com.rtos.model.process;

import com.rtos.common.ProcessStatus;
import com.rtos.model.device.Device;
import javafx.beans.property.*;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Map;
import java.util.Random;

/**
 * @Author: biubiu0002
 * @Date: 2021-08-30 22:20
 * 进程控制块 书本P65
 * 工厂
 *
 **/
@Data
@AllArgsConstructor
public class PCB {

    private IntegerProperty pid;
    private ProcessStatus processStatus;
    private IntegerProperty priority;
    private byte PCBuffer;
    private byte AXBuffer;
    private IntegerProperty restRunTime;
    private IntegerProperty totalRunTime;
    private IntegerProperty lastRunTime;
    private IntegerProperty blockingTime;
    private StringProperty deviceType;

    private byte[] exec;
    private int size;

    public PCB() {
        //防止与闲逛进程1号进程相同PID
        this.pid = new SimpleIntegerProperty((int)(1+Math.random()*512));
        init();
    }

    public PCB(int PID) {
        this.pid=new SimpleIntegerProperty(PID);
        init();

    }

    public int getPID(){
        return this.pid.getValue();
    }

    public IntegerProperty pidProperty(){
        return pid;
    }

    public IntegerProperty totalRunTimeProperty(){
        return totalRunTime;
    }

    public IntegerProperty restRunTimeProperty(){
        return restRunTime;
    }

    public IntegerProperty lastRunTimeProperty(){
        return lastRunTime;
    }

    public IntegerProperty blockingTimeProperty(){
        return blockingTime;
    }

    public StringProperty deviceTypeProperty(){
        return deviceType;
    }

    public double getTotalRunTime(){
        return this.totalRunTime.getValue();
    }

    public double getRestRunTime(){
        return this.restRunTime.getValue();
    }


    public void init(){

        int time = new Random().nextInt(15);
        this.restRunTime = new SimpleIntegerProperty(time);
        this.totalRunTime = new SimpleIntegerProperty(time);
        this.lastRunTime = new SimpleIntegerProperty();
        this.blockingTime = new SimpleIntegerProperty();
        this.deviceType = new SimpleStringProperty();
        this.processStatus=ProcessStatus.CREATED;
        this.priority=new SimpleIntegerProperty(0);
        this.PCBuffer=0;
        this.AXBuffer=0;
    }

    public void init(byte[] instructions){
        int length = instructions.length;
        this.restRunTime = new SimpleIntegerProperty(length);
        this.totalRunTime = new SimpleIntegerProperty(length);
        this.lastRunTime = new SimpleIntegerProperty();
        this.blockingTime = new SimpleIntegerProperty();
        this.deviceType = new SimpleStringProperty();
        this.processStatus=ProcessStatus.CREATED;
        this.priority=new SimpleIntegerProperty(0);
        this.PCBuffer=0;
        this.AXBuffer=0;

    }


}
